﻿#region Opis pliku
//-----------------------------------------------------------------------------
// MainMenu.cs
// Autor: Jan Czarnowski
// 9.11.2012
//-----------------------------------------------------------------------------
#endregion

#region Using...

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;
using System.Collections.Generic;
using System;

#endregion

namespace pogra
{
    /// <summary>
    /// Klasa reprezentująca menu główne
    /// </summary>
    class MainMenu : Menu
    {
        OptionsMenu opMenu;
        bool activatingOptions = false;
        bool deactivatingOptions = false;
        bool inCenter = false;
        TimeSpan timeSinceMoveStarted = TimeSpan.Zero;
        Vector2 moveStartPoint;

        #region inicjalizacja

        public MainMenu()
        {
            title = "{ Main menu }";
        }

        public override void LoadContent()
        {
            Rectangle rectview = ScreenManager.Game.GraphicsDevice.Viewport.Bounds;
            Pos = new Vector2((rectview.Width - menuRect.Width) / 2, (rectview.Height - menuRect.Height) / 2);

            int menuItemHeight = 30;
            int menuItemWidth = menuRect.Width;

            MenuItem startItem = new MenuItem("Start Game", new Rectangle(menuRect.Left, dragRect.Bottom, menuRect.Width, menuItemHeight), this);
            MenuItem optionsItem = new MenuItem("Options", new Rectangle(menuRect.Left, dragRect.Bottom + menuItemHeight, menuRect.Width, menuItemHeight), this);
            MenuItem quitItem = new MenuItem("Quit", new Rectangle(menuRect.Left, dragRect.Bottom + 2 * menuItemHeight, menuRect.Width, menuItemHeight), this);

            itemList.Add(startItem);
            itemList.Add(optionsItem);
            itemList.Add(quitItem);

            opMenu = new OptionsMenu(this);
            ScreenManager.AddScreen(opMenu);
            opMenu.IsActive = false;

            loaded = true;
        }

        #endregion

        public override void Update(GameTime gameTime)
        {
            Vector2 center = new Vector2((ScreenManager.ViewRect.Width-menuRect.Width)/2, (ScreenManager.ViewRect.Height-menuRect.Height)/2);

            base.Update(gameTime);
            
            if(activatingOptions)
            {
                if(inCenter || MoveTo(center, gameTime, TimeSpan.FromSeconds(1)))
                {
                    if (!inCenter)
                    {
                        inCenter = true;
                        timeSinceMoveStarted = TimeSpan.Zero;
                        moveStartPoint = Pos;
                    }

                    if (MoveTo(center - new Vector2(120, 0), gameTime, TimeSpan.FromSeconds(1)))
                    {
                        opMenu.Pos = center + new Vector2(120, 0);
                        opMenu.IsActive = true;
                        activatingOptions = false;
                        inCenter = false;
                    }
                }
            }
            else if(deactivatingOptions)
            {
                if (MoveTo(center, gameTime, TimeSpan.FromSeconds(1)))
                {
                    deactivatingOptions = false;
                    opMenu.IsActive = false;
                }
            }

        }

        #region metody publiczne

        public bool MoveTo(Vector2 point, GameTime gameTime, TimeSpan moveTime)
        {
            timeSinceMoveStarted += gameTime.ElapsedGameTime;

            if (Math.Abs(Pos.X - point.X) < 2 && Math.Abs(Pos.Y - point.Y) < 2)
                return true;

            float a2t2 = (float)(3 / Math.Pow(moveTime.TotalMilliseconds, 2) * Math.Pow(timeSinceMoveStarted.TotalMilliseconds * 4.0f, 2));
            float a3t3 = (float)(2 / Math.Pow(moveTime.TotalMilliseconds, 3) * Math.Pow(timeSinceMoveStarted.TotalMilliseconds * 4.0f, 3));

            Pos = moveStartPoint - Vector2.Multiply(moveStartPoint - point, a2t2) + Vector2.Multiply(moveStartPoint - point, a3t3);

            return false;
        }

        protected override void handleButtons(string pressedText)
        {
            switch (pressedText)
            {
                case "Start Game":
                    GamePlayScreen scrn = new GamePlayScreen();
                    LoadingScreen.Load(ScreenManager, true, scrn, new GamePlayGUI(scrn));
                    break;
                case "Options":
                    if (opMenu.IsActive)
                    {
                        opMenu.IsActive = false;
                        deactivatingOptions = true;
                        timeSinceMoveStarted = TimeSpan.Zero;
                        moveStartPoint = Pos;
                    }
                    else
                    {
                        activatingOptions = true;
                        timeSinceMoveStarted = TimeSpan.Zero;
                        moveStartPoint = Pos;
                       
                        opMenu.Pos = new Vector2(Pos.X + menuRect.Width/2, Pos.Y - menuRect.Height/2) + new Vector2(120, 0);
                    }
                    break;
                case "Quit":
                    ScreenManager.Game.Exit();
                    break;
            }
        }

        #endregion
    }
}